home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / binutils.7 / binutils / binutils-2.7 / bfd / coffgen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-04  |  60.9 KB  |  2,279 lines

  1. /* Support for the generic parts of COFF, for BFD.
  2.    Copyright 1990, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  20.  
  21. /* Most of this hacked by  Steve Chamberlain, sac@cygnus.com.
  22.    Split out of coffcode.h by Ian Taylor, ian@cygnus.com.  */
  23.  
  24. /* This file contains COFF code that is not dependent on any
  25.    particular COFF target.  There is only one version of this file in
  26.    libbfd.a, so no target specific code may be put in here.  Or, to
  27.    put it another way,
  28.  
  29.    ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
  30.  
  31.    If you need to add some target specific behaviour, add a new hook
  32.    function to bfd_coff_backend_data.
  33.  
  34.    Some of these functions are also called by the ECOFF routines.
  35.    Those functions may not use any COFF specific information, such as
  36.    coff_data (abfd).  */
  37.  
  38. #include "bfd.h"
  39. #include "sysdep.h"
  40. #include "libbfd.h"
  41. #include "coff/internal.h"
  42. #include "libcoff.h"
  43.  
  44. static void coff_fix_symbol_name
  45.   PARAMS ((bfd *, asymbol *, combined_entry_type *, bfd_size_type *,
  46.        asection **, bfd_size_type *));
  47. static boolean coff_write_symbol
  48.   PARAMS ((bfd *, asymbol *, combined_entry_type *, unsigned int *,
  49.        bfd_size_type *, asection **, bfd_size_type *));
  50. static boolean coff_write_alien_symbol
  51.   PARAMS ((bfd *, asymbol *, unsigned int *, bfd_size_type *,
  52.        asection **, bfd_size_type *));
  53. static boolean coff_write_native_symbol
  54.   PARAMS ((bfd *, coff_symbol_type *, unsigned int *, bfd_size_type *,
  55.        asection **, bfd_size_type *));
  56. static void coff_pointerize_aux
  57.   PARAMS ((bfd *, combined_entry_type *, combined_entry_type *,
  58.        unsigned int, combined_entry_type *));
  59.  
  60. #define STRING_SIZE_SIZE (4)
  61.  
  62. /* Take a section header read from a coff file (in HOST byte order),
  63.    and make a BFD "section" out of it.  This is used by ECOFF.  */
  64. static boolean
  65. make_a_section_from_file (abfd, hdr, target_index)
  66.      bfd *abfd;
  67.      struct internal_scnhdr *hdr;
  68.      unsigned int target_index;
  69. {
  70.   asection *return_section;
  71.   char *name;
  72.  
  73.   name = NULL;
  74.  
  75.   /* Handle long section names as in PE.  */
  76.   if (bfd_coff_long_section_names (abfd)
  77.       && hdr->s_name[0] == '/')
  78.     {
  79.       char buf[SCNNMLEN];
  80.       long strindex;
  81.       char *p;
  82.       const char *strings;
  83.  
  84.       memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
  85.       buf[SCNNMLEN - 1] = '\0';
  86.       strindex = strtol (buf, &p, 10);
  87.       if (*p == '\0' && strindex >= 0)
  88.     {
  89.       strings = _bfd_coff_read_string_table (abfd);
  90.       if (strings == NULL)
  91.         return false;
  92.       /* FIXME: For extra safety, we should make sure that
  93.              strindex does not run us past the end, but right now we
  94.              don't know the length of the string table.  */
  95.       strings += strindex;
  96.       name = bfd_alloc (abfd, strlen (strings) + 1);
  97.       if (name == NULL)
  98.         return false;
  99.       strcpy (name, strings);
  100.     }
  101.     }
  102.  
  103.   if (name == NULL)
  104.     {
  105.       /* Assorted wastage to null-terminate the name, thanks AT&T! */
  106.       name = bfd_alloc (abfd, sizeof (hdr->s_name) + 1);
  107.       if (name == NULL)
  108.     return false;
  109.       strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
  110.       name[sizeof (hdr->s_name)] = 0;
  111.     }
  112.  
  113.   return_section = bfd_make_section_anyway (abfd, name);
  114.   if (return_section == NULL)
  115.     return false;
  116.  
  117.   return_section->vma = hdr->s_vaddr;
  118.   return_section->lma = hdr->s_paddr;
  119.   return_section->_raw_size = hdr->s_size;
  120.   return_section->filepos = hdr->s_scnptr;
  121.   return_section->rel_filepos = hdr->s_relptr;
  122.   return_section->reloc_count = hdr->s_nreloc;
  123.  
  124.   bfd_coff_set_alignment_hook (abfd, return_section, hdr);
  125.  
  126.   return_section->line_filepos = hdr->s_lnnoptr;
  127.  
  128.   return_section->lineno_count = hdr->s_nlnno;
  129.   return_section->userdata = NULL;
  130.   return_section->next = (asection *) NULL;
  131.   return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name);
  132.  
  133.   return_section->target_index = target_index;
  134.  
  135.   /* At least on i386-coff, the line number count for a shared library
  136.      section must be ignored.  */
  137.   if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
  138.     return_section->lineno_count = 0;
  139.  
  140.   if (hdr->s_nreloc != 0)
  141.     return_section->flags |= SEC_RELOC;
  142.   /* FIXME: should this check 'hdr->s_size > 0' */
  143.   if (hdr->s_scnptr != 0)
  144.     return_section->flags |= SEC_HAS_CONTENTS;
  145.   return true;
  146. }
  147.  
  148. /* Read in a COFF object and make it into a BFD.  This is used by
  149.    ECOFF as well.  */
  150.  
  151. static const bfd_target *
  152. coff_real_object_p (abfd, nscns, internal_f, internal_a)
  153.      bfd *abfd;
  154.      unsigned nscns;
  155.      struct internal_filehdr *internal_f;
  156.      struct internal_aouthdr *internal_a;
  157. {
  158.   flagword oflags = abfd->flags;
  159.   bfd_vma ostart = bfd_get_start_address (abfd);
  160.   PTR tdata;
  161.   size_t readsize;        /* length of file_info */
  162.   unsigned int scnhsz;
  163.   char *external_sections;
  164.  
  165.   if (!(internal_f->f_flags & F_RELFLG))
  166.     abfd->flags |= HAS_RELOC;
  167.   if ((internal_f->f_flags & F_EXEC))
  168.     abfd->flags |= EXEC_P;
  169.   if (!(internal_f->f_flags & F_LNNO))
  170.     abfd->flags |= HAS_LINENO;
  171.   if (!(internal_f->f_flags & F_LSYMS))
  172.     abfd->flags |= HAS_LOCALS;
  173.  
  174.   /* FIXME: How can we set D_PAGED correctly?  */
  175.   if ((internal_f->f_flags & F_EXEC) != 0)
  176.     abfd->flags |= D_PAGED;
  177.  
  178.   bfd_get_symcount (abfd) = internal_f->f_nsyms;
  179.   if (internal_f->f_nsyms)
  180.     abfd->flags |= HAS_SYMS;
  181.  
  182.   if (internal_a != (struct internal_aouthdr *) NULL)
  183.     bfd_get_start_address (abfd) = internal_a->entry;
  184.   else
  185.     bfd_get_start_address (abfd) = 0;
  186.  
  187.   /* Set up the tdata area.  ECOFF uses its own routine, and overrides
  188.      abfd->flags.  */
  189.   tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
  190.   if (tdata == NULL)
  191.     return 0;
  192.  
  193.   scnhsz = bfd_coff_scnhsz (abfd);
  194.   readsize = nscns * scnhsz;
  195.   external_sections = (char *) bfd_alloc (abfd, readsize);
  196.   if (!external_sections)
  197.     goto fail;
  198.  
  199.   if (bfd_read ((PTR) external_sections, 1, readsize, abfd) != readsize)
  200.     goto fail;
  201.  
  202.   /* Now copy data as required; construct all asections etc */
  203.   if (nscns != 0)
  204.     {
  205.       unsigned int i;
  206.       for (i = 0; i < nscns; i++)
  207.     {
  208.       struct internal_scnhdr tmp;
  209.       bfd_coff_swap_scnhdr_in (abfd,
  210.                    (PTR) (external_sections + i * scnhsz),
  211.                    (PTR) & tmp);
  212.       if (! make_a_section_from_file (abfd, &tmp, i + 1))
  213.         goto fail;
  214.     }
  215.     }
  216.  
  217.   /*  make_abs_section (abfd); */
  218.  
  219.   if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
  220.     goto fail;
  221.  
  222.   return abfd->xvec;
  223.  
  224.  fail:
  225.   bfd_release (abfd, tdata);
  226.   abfd->flags = oflags;
  227.   bfd_get_start_address (abfd) = ostart;
  228.   return (const bfd_target *) NULL;
  229. }
  230.  
  231. /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
  232.    not a COFF file.  This is also used by ECOFF.  */
  233.  
  234. const bfd_target *
  235. coff_object_p (abfd)
  236.      bfd *abfd;
  237. {
  238.   unsigned int filhsz;
  239.   unsigned int aoutsz;
  240.   int nscns;
  241.   PTR filehdr;
  242.   struct internal_filehdr internal_f;
  243.   struct internal_aouthdr internal_a;
  244.  
  245.   /* figure out how much to read */
  246.   filhsz = bfd_coff_filhsz (abfd);
  247.   aoutsz = bfd_coff_aoutsz (abfd);
  248.  
  249.   filehdr = bfd_alloc (abfd, filhsz);
  250.   if (filehdr == NULL)
  251.     return 0;
  252.   if (bfd_read (filehdr, 1, filhsz, abfd) != filhsz)
  253.     {
  254.       if (bfd_get_error () != bfd_error_system_call)
  255.     bfd_set_error (bfd_error_wrong_format);
  256.       return 0;
  257.     }
  258.   bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
  259.   bfd_release (abfd, filehdr);
  260.  
  261.   if (bfd_coff_bad_format_hook (abfd, &internal_f) == false)
  262.     {
  263.       bfd_set_error (bfd_error_wrong_format);
  264.       return 0;
  265.     }
  266.   nscns = internal_f.f_nscns;
  267.  
  268.   if (internal_f.f_opthdr)
  269.     {
  270.       PTR opthdr;
  271.  
  272.       opthdr = bfd_alloc (abfd, aoutsz);
  273.       if (opthdr == NULL)
  274.     return 0;;
  275.       if (bfd_read (opthdr, 1, aoutsz, abfd) != aoutsz)
  276.     {
  277.       return 0;
  278.     }
  279.       bfd_coff_swap_aouthdr_in (abfd, opthdr, (PTR) & internal_a);
  280.     }
  281.  
  282.   /* Seek past the opt hdr stuff */
  283.   if (bfd_seek (abfd, (file_ptr) (internal_f.f_opthdr + filhsz), SEEK_SET)
  284.       != 0)
  285.     return NULL;
  286.  
  287.   return coff_real_object_p (abfd, nscns, &internal_f,
  288.                  (internal_f.f_opthdr != 0
  289.                   ? &internal_a
  290.                   : (struct internal_aouthdr *) NULL));
  291. }
  292.  
  293. /* Get the BFD section from a COFF symbol section number.  */
  294.  
  295. asection *
  296. coff_section_from_bfd_index (abfd, index)
  297.      bfd *abfd;
  298.      int index;
  299. {
  300.   struct sec *answer = abfd->sections;
  301.  
  302.   if (index == N_ABS)
  303.     return bfd_abs_section_ptr;
  304.   if (index == N_UNDEF)
  305.     return bfd_und_section_ptr;
  306.   if (index == N_DEBUG)
  307.     return bfd_abs_section_ptr;
  308.  
  309.   while (answer)
  310.     {
  311.       if (answer->target_index == index)
  312.     return answer;
  313.       answer = answer->next;
  314.     }
  315.  
  316.   /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
  317.      has a bad symbol table in biglitpow.o.  */
  318.   return bfd_und_section_ptr;
  319. }
  320.  
  321. /* Get the upper bound of a COFF symbol table.  */
  322.  
  323. long
  324. coff_get_symtab_upper_bound (abfd)
  325.      bfd *abfd;
  326. {
  327.   if (!bfd_coff_slurp_symbol_table (abfd))
  328.     return -1;
  329.  
  330.   return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
  331. }
  332.  
  333.  
  334. /* Canonicalize a COFF symbol table.  */
  335.  
  336. long
  337. coff_get_symtab (abfd, alocation)
  338.      bfd *abfd;
  339.      asymbol **alocation;
  340. {
  341.   unsigned int counter;
  342.   coff_symbol_type *symbase;
  343.   coff_symbol_type **location = (coff_symbol_type **) alocation;
  344.  
  345.   if (!bfd_coff_slurp_symbol_table (abfd))
  346.     return -1;
  347.  
  348.   symbase = obj_symbols (abfd);
  349.   counter = bfd_get_symcount (abfd);
  350.   while (counter-- > 0)
  351.     *location++ = symbase++;
  352.  
  353.   *location = NULL;
  354.  
  355.   return bfd_get_symcount (abfd);
  356. }
  357.  
  358. /* Get the name of a symbol.  The caller must pass in a buffer of size
  359.    >= SYMNMLEN + 1.  */
  360.  
  361. const char *
  362. _bfd_coff_internal_syment_name (abfd, sym, buf)
  363.      bfd *abfd;
  364.      const struct internal_syment *sym;
  365.      char *buf;
  366. {
  367.   /* FIXME: It's not clear this will work correctly if sizeof
  368.      (_n_zeroes) != 4.  */
  369.   if (sym->_n._n_n._n_zeroes != 0
  370.       || sym->_n._n_n._n_offset == 0)
  371.     {
  372.       memcpy (buf, sym->_n._n_name, SYMNMLEN);
  373.       buf[SYMNMLEN] = '\0';
  374.       return buf;
  375.     }
  376.   else
  377.     {
  378.       const char *strings;
  379.  
  380.       BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
  381.       strings = obj_coff_strings (abfd);
  382.       if (strings == NULL)
  383.     {
  384.       strings = _bfd_coff_read_string_table (abfd);
  385.       if (strings == NULL)
  386.         return NULL;
  387.     }
  388.       return strings + sym->_n._n_n._n_offset;
  389.     }
  390. }
  391.  
  392. /* Read in and swap the relocs.  This returns a buffer holding the
  393.    relocs for section SEC in file ABFD.  If CACHE is true and
  394.    INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
  395.    the function is called again.  If EXTERNAL_RELOCS is not NULL, it
  396.    is a buffer large enough to hold the unswapped relocs.  If
  397.    INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
  398.    the swapped relocs.  If REQUIRE_INTERNAL is true, then the return
  399.    value must be INTERNAL_RELOCS.  The function returns NULL on error.  */
  400.  
  401. struct internal_reloc *
  402. _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
  403.                 require_internal, internal_relocs)
  404.      bfd *abfd;
  405.      asection *sec;
  406.      boolean cache;
  407.      bfd_byte *external_relocs;
  408.      boolean require_internal;
  409.      struct internal_reloc *internal_relocs;
  410. {
  411.   bfd_size_type relsz;
  412.   bfd_byte *free_external = NULL;
  413.   struct internal_reloc *free_internal = NULL;
  414.   bfd_byte *erel;
  415.   bfd_byte *erel_end;
  416.   struct internal_reloc *irel;
  417.  
  418.   if (coff_section_data (abfd, sec) != NULL
  419.       && coff_section_data (abfd, sec)->relocs != NULL)
  420.     {
  421.       if (! require_internal)
  422.     return coff_section_data (abfd, sec)->relocs;
  423.       memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
  424.           sec->reloc_count * sizeof (struct internal_reloc));
  425.       return internal_relocs;
  426.     }
  427.  
  428.   relsz = bfd_coff_relsz (abfd);
  429.  
  430.   if (external_relocs == NULL)
  431.     {
  432.       free_external = (bfd_byte *) bfd_malloc (sec->reloc_count * relsz);
  433.       if (free_external == NULL && sec->reloc_count > 0)
  434.     goto error_return;
  435.       external_relocs = free_external;
  436.     }
  437.  
  438.   if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
  439.       || (bfd_read (external_relocs, relsz, sec->reloc_count, abfd)
  440.       != relsz * sec->reloc_count))
  441.     goto error_return;
  442.  
  443.   if (internal_relocs == NULL)
  444.     {
  445.       free_internal = ((struct internal_reloc *)
  446.                bfd_malloc (sec->reloc_count
  447.                    * sizeof (struct internal_reloc)));
  448.       if (free_internal == NULL && sec->reloc_count > 0)
  449.     goto error_return;
  450.       internal_relocs = free_internal;
  451.     }
  452.  
  453.   /* Swap in the relocs.  */
  454.   erel = external_relocs;
  455.   erel_end = erel + relsz * sec->reloc_count;
  456.   irel = internal_relocs;
  457.   for (; erel < erel_end; erel += relsz, irel++)
  458.     bfd_coff_swap_reloc_in (abfd, (PTR) erel, (PTR) irel);
  459.  
  460.   if (free_external != NULL)
  461.     {
  462.       free (free_external);
  463.       free_external = NULL;
  464.     }
  465.  
  466.   if (cache && free_internal != NULL)
  467.     {
  468.       if (coff_section_data (abfd, sec) == NULL)
  469.     {
  470.       sec->used_by_bfd =
  471.         (PTR) bfd_zalloc (abfd,
  472.                   sizeof (struct coff_section_tdata));
  473.       if (sec->used_by_bfd == NULL)
  474.         goto error_return;
  475.       coff_section_data (abfd, sec)->contents = NULL;
  476.     }
  477.       coff_section_data (abfd, sec)->relocs = free_internal;
  478.     }
  479.  
  480.   return internal_relocs;
  481.  
  482.  error_return:
  483.   if (free_external != NULL)
  484.     free (free_external);
  485.   if (free_internal != NULL)
  486.     free (free_internal);
  487.   return NULL;
  488. }
  489.  
  490. /* Set lineno_count for the output sections of a COFF file.  */
  491.  
  492. int
  493. coff_count_linenumbers (abfd)
  494.      bfd *abfd;
  495. {
  496.   unsigned int limit = bfd_get_symcount (abfd);
  497.   unsigned int i;
  498.   int total = 0;
  499.   asymbol **p;
  500.   asection *s;
  501.  
  502.   if (limit == 0)
  503.     {
  504.       /* This may be from the backend linker, in which case the
  505.          lineno_count in the sections is correct.  */
  506.       for (s = abfd->sections; s != NULL; s = s->next)
  507.     total += s->lineno_count;
  508.       return total;
  509.     }
  510.  
  511.   for (s = abfd->sections; s != NULL; s = s->next)
  512.     BFD_ASSERT (s->lineno_count == 0);
  513.  
  514.   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
  515.     {
  516.       asymbol *q_maybe = *p;
  517.  
  518.       if (bfd_asymbol_flavour (q_maybe) == bfd_target_coff_flavour)
  519.     {
  520.       coff_symbol_type *q = coffsymbol (q_maybe);
  521.  
  522.       /* The AIX 4.1 compiler can sometimes generate line numbers
  523.              attached to debugging symbols.  We try to simply ignore
  524.              those here.  */
  525.       if (q->lineno != NULL
  526.           && q->symbol.section->owner != NULL)
  527.         {
  528.           /* This symbol has line numbers.  Increment the owning
  529.              section's linenumber count.  */
  530.           alent *l = q->lineno;
  531.  
  532.           ++q->symbol.section->output_section->lineno_count;
  533.           ++total;
  534.           ++l;
  535.           while (l->line_number != 0)
  536.         {
  537.           ++total;
  538.           ++q->symbol.section->output_section->lineno_count;
  539.           ++l;
  540.         }
  541.         }
  542.     }
  543.     }
  544.  
  545.   return total;
  546. }
  547.  
  548. /* Takes a bfd and a symbol, returns a pointer to the coff specific
  549.    area of the symbol if there is one.  */
  550.  
  551. /*ARGSUSED*/
  552. coff_symbol_type *
  553. coff_symbol_from (ignore_abfd, symbol)
  554.      bfd *ignore_abfd;
  555.      asymbol *symbol;
  556. {
  557.   if (bfd_asymbol_flavour (symbol) != bfd_target_coff_flavour)
  558.     return (coff_symbol_type *) NULL;
  559.  
  560.   if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
  561.     return (coff_symbol_type *) NULL;
  562.  
  563.   return (coff_symbol_type *) symbol;
  564. }
  565.  
  566. static void
  567. fixup_symbol_value (coff_symbol_ptr, syment)
  568.      coff_symbol_type *coff_symbol_ptr;
  569.      struct internal_syment *syment;
  570. {
  571.  
  572.   /* Normalize the symbol flags */
  573.   if (bfd_is_com_section (coff_symbol_ptr->symbol.section))
  574.     {
  575.       /* a common symbol is undefined with a value */
  576.       syment->n_scnum = N_UNDEF;
  577.       syment->n_value = coff_symbol_ptr->symbol.value;
  578.     }
  579.   else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING)
  580.     {
  581.       syment->n_value = coff_symbol_ptr->symbol.value;
  582.     }
  583.   else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
  584.     {
  585.       syment->n_scnum = N_UNDEF;
  586.       syment->n_value = 0;
  587.     }
  588.   else
  589.     {
  590.       if (coff_symbol_ptr->symbol.section)
  591.     {
  592.       syment->n_scnum =
  593.         coff_symbol_ptr->symbol.section->output_section->target_index;
  594.  
  595.       syment->n_value =
  596.         coff_symbol_ptr->symbol.value +
  597.         coff_symbol_ptr->symbol.section->output_offset +
  598.         coff_symbol_ptr->symbol.section->output_section->vma;
  599.     }
  600.       else
  601.     {
  602.       BFD_ASSERT (0);
  603.       /* This can happen, but I don't know why yet (steve@cygnus.com) */
  604.       syment->n_scnum = N_ABS;
  605.       syment->n_value = coff_symbol_ptr->symbol.value;
  606.     }
  607.     }
  608. }
  609.  
  610. /* Run through all the symbols in the symbol table and work out what
  611.    their indexes into the symbol table will be when output.
  612.  
  613.    Coff requires that each C_FILE symbol points to the next one in the
  614.    chain, and that the last one points to the first external symbol. We
  615.    do that here too.  */
  616.  
  617. boolean
  618. coff_renumber_symbols (bfd_ptr, first_undef)
  619.      bfd *bfd_ptr;
  620.      int *first_undef;
  621. {
  622.   unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
  623.   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
  624.   unsigned int native_index = 0;
  625.   struct internal_syment *last_file = (struct internal_syment *) NULL;
  626.   unsigned int symbol_index;
  627.  
  628.   /* COFF demands that undefined symbols come after all other symbols.
  629.      Since we don't need to impose this extra knowledge on all our
  630.      client programs, deal with that here.  Sort the symbol table;
  631.      just move the undefined symbols to the end, leaving the rest
  632.      alone.  The O'Reilly book says that defined global symbols come
  633.      at the end before the undefined symbols, so we do that here as
  634.      well.  */
  635.   /* @@ Do we have some condition we could test for, so we don't always
  636.      have to do this?  I don't think relocatability is quite right, but
  637.      I'm not certain.  [raeburn:19920508.1711EST]  */
  638.   {
  639.     asymbol **newsyms;
  640.     unsigned int i;
  641.  
  642.     newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr,
  643.                         sizeof (asymbol *)
  644.                         * (symbol_count + 1));
  645.     if (!newsyms)
  646.       return false;
  647.     bfd_ptr->outsymbols = newsyms;
  648.     for (i = 0; i < symbol_count; i++)
  649.       if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
  650.       || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
  651.           && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
  652.           && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_FUNCTION))
  653.           != BSF_GLOBAL)))
  654.     *newsyms++ = symbol_ptr_ptr[i];
  655.  
  656.     for (i = 0; i < symbol_count; i++)
  657.       if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
  658.       && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
  659.       && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
  660.           || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_FUNCTION))
  661.           == BSF_GLOBAL)))
  662.     *newsyms++ = symbol_ptr_ptr[i];
  663.  
  664.     *first_undef = newsyms - bfd_ptr->outsymbols;
  665.  
  666.     for (i = 0; i < symbol_count; i++)
  667.       if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
  668.       && bfd_is_und_section (symbol_ptr_ptr[i]->section))
  669.     *newsyms++ = symbol_ptr_ptr[i];
  670.     *newsyms = (asymbol *) NULL;
  671.     symbol_ptr_ptr = bfd_ptr->outsymbols;
  672.   }
  673.  
  674.   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
  675.     {
  676.       coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
  677.       symbol_ptr_ptr[symbol_index]->udata.i = symbol_index; 
  678.       if (coff_symbol_ptr && coff_symbol_ptr->native)
  679.     {
  680.       combined_entry_type *s = coff_symbol_ptr->native;
  681.       int i;
  682.  
  683.       if (s->u.syment.n_sclass == C_FILE)
  684.         {
  685.           if (last_file != (struct internal_syment *) NULL)
  686.         last_file->n_value = native_index;
  687.           last_file = &(s->u.syment);
  688.         }
  689.       else
  690.         {
  691.  
  692.           /* Modify the symbol values according to their section and
  693.              type */
  694.  
  695.           fixup_symbol_value (coff_symbol_ptr, &(s->u.syment));
  696.         }
  697.       for (i = 0; i < s->u.syment.n_numaux + 1; i++)
  698.         s[i].offset = native_index++;
  699.     }
  700.       else
  701.     {
  702.       native_index++;
  703.     }
  704.     }
  705.   obj_conv_table_size (bfd_ptr) = native_index;
  706.  
  707.   return true;
  708. }
  709.  
  710. /* Run thorough the symbol table again, and fix it so that all
  711.    pointers to entries are changed to the entries' index in the output
  712.    symbol table.  */
  713.  
  714. void
  715. coff_mangle_symbols (bfd_ptr)
  716.      bfd *bfd_ptr;
  717. {
  718.   unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
  719.   asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
  720.   unsigned int symbol_index;
  721.  
  722.   for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
  723.     {
  724.       coff_symbol_type *coff_symbol_ptr =
  725.       coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
  726.  
  727.       if (coff_symbol_ptr && coff_symbol_ptr->native)
  728.     {
  729.       int i;
  730.       combined_entry_type *s = coff_symbol_ptr->native;
  731.  
  732.       if (s->fix_value)
  733.         {
  734.           /* FIXME: We should use a union here.  */
  735.           s->u.syment.n_value =
  736.         ((combined_entry_type *) s->u.syment.n_value)->offset;
  737.           s->fix_value = 0;
  738.         }
  739.       if (s->fix_line)
  740.         {
  741.           /* The value is the offset into the line number entries
  742.                  for the symbol's section.  On output, the symbol's
  743.                  section should be N_DEBUG.  */
  744.           s->u.syment.n_value =
  745.         (coff_symbol_ptr->symbol.section->output_section->line_filepos
  746.          + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
  747.           coff_symbol_ptr->symbol.section =
  748.         coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
  749.           BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
  750.         }
  751.       for (i = 0; i < s->u.syment.n_numaux; i++)
  752.         {
  753.           combined_entry_type *a = s + i + 1;
  754.           if (a->fix_tag)
  755.         {
  756.           a->u.auxent.x_sym.x_tagndx.l =
  757.             a->u.auxent.x_sym.x_tagndx.p->offset;
  758.           a->fix_tag = 0;
  759.         }
  760.           if (a->fix_end)
  761.         {
  762.           a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
  763.             a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
  764.           a->fix_end = 0;
  765.         }
  766.           if (a->fix_scnlen)
  767.         {
  768.           a->u.auxent.x_csect.x_scnlen.l =
  769.             a->u.auxent.x_csect.x_scnlen.p->offset;
  770.           a->fix_scnlen = 0;
  771.         }
  772.         }
  773.     }
  774.     }
  775. }
  776.  
  777. static void
  778. coff_fix_symbol_name (abfd, symbol, native, string_size_p,
  779.               debug_string_section_p, debug_string_size_p)
  780.      bfd *abfd;
  781.      asymbol *symbol;
  782.      combined_entry_type *native;
  783.      bfd_size_type *string_size_p;
  784.      asection **debug_string_section_p;
  785.      bfd_size_type *debug_string_size_p;
  786. {
  787.   unsigned int name_length;
  788.   union internal_auxent *auxent;
  789.   char *name = (char *) (symbol->name);
  790.  
  791.   if (name == (char *) NULL)
  792.     {
  793.       /* coff symbols always have names, so we'll make one up */
  794.       symbol->name = "strange";
  795.       name = (char *) symbol->name;
  796.     }
  797.   name_length = strlen (name);
  798.  
  799.   if (native->u.syment.n_sclass == C_FILE
  800.       && native->u.syment.n_numaux > 0)
  801.     {
  802.       strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
  803.       auxent = &(native + 1)->u.auxent;
  804.  
  805.       if (bfd_coff_long_filenames (abfd))
  806.     {
  807.       if (name_length <= FILNMLEN)
  808.         {
  809.           strncpy (auxent->x_file.x_fname, name, FILNMLEN);
  810.         }
  811.       else
  812.         {
  813.           auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
  814.           auxent->x_file.x_n.x_zeroes = 0;
  815.           *string_size_p += name_length + 1;
  816.         }
  817.     }
  818.       else
  819.     {
  820.       strncpy (auxent->x_file.x_fname, name, FILNMLEN);
  821.       if (name_length > FILNMLEN)
  822.         {
  823.           name[FILNMLEN] = '\0';
  824.         }
  825.     }
  826.     }
  827.   else
  828.     {
  829.       if (name_length <= SYMNMLEN)
  830.     {
  831.       /* This name will fit into the symbol neatly */
  832.       strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
  833.     }
  834.       else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
  835.     {
  836.       native->u.syment._n._n_n._n_offset = (*string_size_p
  837.                         + STRING_SIZE_SIZE);
  838.       native->u.syment._n._n_n._n_zeroes = 0;
  839.       *string_size_p += name_length + 1;
  840.     }
  841.       else
  842.     {
  843.       long filepos;
  844.       bfd_byte buf[2];
  845.  
  846.       /* This name should be written into the .debug section.  For
  847.          some reason each name is preceded by a two byte length
  848.          and also followed by a null byte.  FIXME: We assume that
  849.          the .debug section has already been created, and that it
  850.          is large enough.  */
  851.       if (*debug_string_section_p == (asection *) NULL)
  852.         *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
  853.       filepos = bfd_tell (abfd);
  854.       bfd_put_16 (abfd, name_length + 1, buf);
  855.       if (!bfd_set_section_contents (abfd,
  856.                      *debug_string_section_p,
  857.                      (PTR) buf,
  858.                      (file_ptr) *debug_string_size_p,
  859.                      (bfd_size_type) 2)
  860.           || !bfd_set_section_contents (abfd,
  861.                         *debug_string_section_p,
  862.                         (PTR) symbol->name,
  863.                         ((file_ptr) *debug_string_size_p
  864.                          + 2),
  865.                         (bfd_size_type) name_length + 1))
  866.         abort ();
  867.       if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
  868.         abort ();
  869.       native->u.syment._n._n_n._n_offset = *debug_string_size_p + 2;
  870.       native->u.syment._n._n_n._n_zeroes = 0;
  871.       *debug_string_size_p += name_length + 3;
  872.     }
  873.     }
  874. }
  875.  
  876. /* We need to keep track of the symbol index so that when we write out
  877.    the relocs we can get the index for a symbol.  This method is a
  878.    hack.  FIXME.  */
  879.  
  880. #define set_index(symbol, idx)    ((symbol)->udata.i = (idx))
  881.  
  882. /* Write a symbol out to a COFF file.  */
  883.  
  884. static boolean
  885. coff_write_symbol (abfd, symbol, native, written, string_size_p,
  886.            debug_string_section_p, debug_string_size_p)
  887.      bfd *abfd;
  888.      asymbol *symbol;
  889.      combined_entry_type *native;
  890.      unsigned int *written;
  891.      bfd_size_type *string_size_p;
  892.      asection **debug_string_section_p;
  893.      bfd_size_type *debug_string_size_p;
  894. {
  895.   unsigned int numaux = native->u.syment.n_numaux;
  896.   int type = native->u.syment.n_type;
  897.   int class = native->u.syment.n_sclass;
  898.   PTR buf;
  899.   bfd_size_type symesz;
  900.  
  901.   if (native->u.syment.n_sclass == C_FILE)
  902.     symbol->flags |= BSF_DEBUGGING;
  903.  
  904.   if (symbol->flags & BSF_DEBUGGING
  905.       && bfd_is_abs_section (symbol->section))
  906.     {
  907.       native->u.syment.n_scnum = N_DEBUG;
  908.     }
  909.   else if (bfd_is_abs_section (symbol->section))
  910.     {
  911.       native->u.syment.n_scnum = N_ABS;
  912.     }
  913.   else if (bfd_is_und_section (symbol->section))
  914.     {
  915.       native->u.syment.n_scnum = N_UNDEF;
  916.     }
  917.   else
  918.     {
  919.       native->u.syment.n_scnum =
  920.     symbol->section->output_section->target_index;
  921.     }
  922.  
  923.   coff_fix_symbol_name (abfd, symbol, native, string_size_p,
  924.             debug_string_section_p, debug_string_size_p);
  925.  
  926.   symesz = bfd_coff_symesz (abfd);
  927.   buf = bfd_alloc (abfd, symesz);
  928.   if (!buf)
  929.     return false;
  930.   bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
  931.   if (bfd_write (buf, 1, symesz, abfd) != symesz)
  932.     return false;
  933.   bfd_release (abfd, buf);
  934.  
  935.   if (native->u.syment.n_numaux > 0)
  936.     {
  937.       bfd_size_type auxesz;
  938.       unsigned int j;
  939.  
  940.       auxesz = bfd_coff_auxesz (abfd);
  941.       buf = bfd_alloc (abfd, auxesz);
  942.       if (!buf)
  943.     return false;
  944.       for (j = 0; j < native->u.syment.n_numaux; j++)
  945.     {
  946.       bfd_coff_swap_aux_out (abfd,
  947.                  &((native + j + 1)->u.auxent),
  948.                  type,
  949.                  class,
  950.                  j,
  951.                  native->u.syment.n_numaux,
  952.                  buf);
  953.       if (bfd_write (buf, 1, auxesz, abfd) != auxesz)
  954.         return false;
  955.     }
  956.       bfd_release (abfd, buf);
  957.     }
  958.  
  959.   /* Store the index for use when we write out the relocs.  */
  960.   set_index (symbol, *written);
  961.  
  962.   *written += numaux + 1;
  963.   return true;
  964. }
  965.  
  966. /* Write out a symbol to a COFF file that does not come from a COFF
  967.    file originally.  This symbol may have been created by the linker,
  968.    or we may be linking a non COFF file to a COFF file.  */
  969.  
  970. static boolean
  971. coff_write_alien_symbol (abfd, symbol, written, string_size_p,
  972.              debug_string_section_p, debug_string_size_p)
  973.      bfd *abfd;
  974.      asymbol *symbol;
  975.      unsigned int *written;
  976.      bfd_size_type *string_size_p;
  977.      asection **debug_string_section_p;
  978.      bfd_size_type *debug_string_size_p;
  979. {
  980.   combined_entry_type *native;
  981.   combined_entry_type dummy;
  982.  
  983.   native = &dummy;
  984.   native->u.syment.n_type = T_NULL;
  985.   native->u.syment.n_flags = 0;
  986.   if (bfd_is_und_section (symbol->section))
  987.     {
  988.       native->u.syment.n_scnum = N_UNDEF;
  989.       native->u.syment.n_value = symbol->value;
  990.     }
  991.   else if (bfd_is_com_section (symbol->section))
  992.     {
  993.       native->u.syment.n_scnum = N_UNDEF;
  994.       native->u.syment.n_value = symbol->value;
  995.     }
  996.   else if (symbol->flags & BSF_DEBUGGING)
  997.     {
  998.       /* There isn't much point to writing out a debugging symbol
  999.          unless we are prepared to convert it into COFF debugging
  1000.          format.  So, we just ignore them.  We must clobber the symbol
  1001.          name to keep it from being put in the string table.  */
  1002.       symbol->name = "";
  1003.       return true;
  1004.     }
  1005.   else
  1006.     {
  1007.       native->u.syment.n_scnum =
  1008.     symbol->section->output_section->target_index;
  1009.       native->u.syment.n_value = (symbol->value
  1010.                   + symbol->section->output_section->vma
  1011.                   + symbol->section->output_offset);
  1012.  
  1013.       /* Copy the any flags from the the file header into the symbol.
  1014.          FIXME: Why?  */
  1015.       {
  1016.     coff_symbol_type *c = coff_symbol_from (abfd, symbol);
  1017.     if (c != (coff_symbol_type *) NULL)
  1018.       native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
  1019.       }
  1020.     }
  1021.  
  1022.   native->u.syment.n_type = 0;
  1023.   if (symbol->flags & BSF_LOCAL)
  1024.     native->u.syment.n_sclass = C_STAT;
  1025.   else
  1026.     native->u.syment.n_sclass = C_EXT;
  1027.   native->u.syment.n_numaux = 0;
  1028.  
  1029.   return coff_write_symbol (abfd, symbol, native, written, string_size_p,
  1030.                 debug_string_section_p, debug_string_size_p);
  1031. }
  1032.  
  1033. /* Write a native symbol to a COFF file.  */
  1034.  
  1035. static boolean
  1036. coff_write_native_symbol (abfd, symbol, written, string_size_p,
  1037.               debug_string_section_p, debug_string_size_p)
  1038.      bfd *abfd;
  1039.      coff_symbol_type *symbol;
  1040.      unsigned int *written;
  1041.      bfd_size_type *string_size_p;
  1042.      asection **debug_string_section_p;
  1043.      bfd_size_type *debug_string_size_p;
  1044. {
  1045.   combined_entry_type *native = symbol->native;
  1046.   alent *lineno = symbol->lineno;
  1047.  
  1048.   /* If this symbol has an associated line number, we must store the
  1049.      symbol index in the line number field.  We also tag the auxent to
  1050.      point to the right place in the lineno table.  */
  1051.   if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
  1052.     {
  1053.       unsigned int count = 0;
  1054.       lineno[count].u.offset = *written;
  1055.       if (native->u.syment.n_numaux)
  1056.     {
  1057.       union internal_auxent *a = &((native + 1)->u.auxent);
  1058.  
  1059.       a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
  1060.         symbol->symbol.section->output_section->moving_line_filepos;
  1061.     }
  1062.  
  1063.       /* Count and relocate all other linenumbers.  */
  1064.       count++;
  1065.       while (lineno[count].line_number != 0)
  1066.     {
  1067. #if 0
  1068.       /* 13 april 92. sac 
  1069.          I've been told this, but still need proof:
  1070.          > The second bug is also in `bfd/coffcode.h'.  This bug
  1071.          > causes the linker to screw up the pc-relocations for
  1072.          > all the line numbers in COFF code.  This bug isn't only
  1073.          > specific to A29K implementations, but affects all
  1074.          > systems using COFF format binaries.  Note that in COFF
  1075.          > object files, the line number core offsets output by
  1076.          > the assembler are relative to the start of each
  1077.          > procedure, not to the start of the .text section.  This
  1078.          > patch relocates the line numbers relative to the
  1079.          > `native->u.syment.n_value' instead of the section
  1080.          > virtual address.
  1081.          > modular!olson@cs.arizona.edu (Jon Olson)
  1082.        */
  1083.       lineno[count].u.offset += native->u.syment.n_value;
  1084. #else
  1085.       lineno[count].u.offset +=
  1086.         (symbol->symbol.section->output_section->vma
  1087.          + symbol->symbol.section->output_offset);
  1088. #endif
  1089.       count++;
  1090.     }
  1091.       symbol->done_lineno = true;
  1092.  
  1093.       symbol->symbol.section->output_section->moving_line_filepos +=
  1094.     count * bfd_coff_linesz (abfd);
  1095.     }
  1096.  
  1097.   return coff_write_symbol (abfd, &(symbol->symbol), native, written,
  1098.                 string_size_p, debug_string_section_p,
  1099.                 debug_string_size_p);
  1100. }
  1101.  
  1102. /* Write out the COFF symbols.  */
  1103.  
  1104. boolean
  1105. coff_write_symbols (abfd)
  1106.      bfd *abfd;
  1107. {
  1108.   bfd_size_type string_size;
  1109.   asection *debug_string_section;
  1110.   bfd_size_type debug_string_size;
  1111.   unsigned int i;
  1112.   unsigned int limit = bfd_get_symcount (abfd);
  1113.   unsigned int written = 0;
  1114.   asymbol **p;
  1115.  
  1116.   string_size = 0;
  1117.   debug_string_section = NULL;
  1118.   debug_string_size = 0;
  1119.  
  1120.   /* If this target supports long section names, they must be put into
  1121.      the string table.  This is supported by PE.  This code must
  1122.      handle section names just as they are handled in
  1123.      coff_write_object_contents.  */
  1124.   if (bfd_coff_long_section_names (abfd))
  1125.     {
  1126.       asection *o;
  1127.  
  1128.       for (o = abfd->sections; o != NULL; o = o->next)
  1129.     {
  1130.       size_t len;
  1131.  
  1132.       len = strlen (o->name);
  1133.       if (len > SCNNMLEN)
  1134.         string_size += len + 1;
  1135.     }
  1136.     }
  1137.  
  1138.   /* Seek to the right place */
  1139.   if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
  1140.     return false;
  1141.  
  1142.   /* Output all the symbols we have */
  1143.  
  1144.   written = 0;
  1145.   for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
  1146.     {
  1147.       asymbol *symbol = *p;
  1148.       coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
  1149.  
  1150.       if (c_symbol == (coff_symbol_type *) NULL
  1151.       || c_symbol->native == (combined_entry_type *) NULL)
  1152.     {
  1153.       if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
  1154.                     &debug_string_section,
  1155.                     &debug_string_size))
  1156.         return false;
  1157.     }
  1158.       else
  1159.     {
  1160.       if (!coff_write_native_symbol (abfd, c_symbol, &written,
  1161.                      &string_size, &debug_string_section,
  1162.                      &debug_string_size))
  1163.         return false;
  1164.     }
  1165.     }
  1166.  
  1167.   obj_raw_syment_count (abfd) = written;
  1168.  
  1169.   /* Now write out strings */
  1170.  
  1171.   if (string_size != 0)
  1172.     {
  1173.       unsigned int size = string_size + STRING_SIZE_SIZE;
  1174.       bfd_byte buffer[STRING_SIZE_SIZE];
  1175.  
  1176. #if STRING_SIZE_SIZE == 4
  1177.       bfd_h_put_32 (abfd, size, buffer);
  1178. #else
  1179.  #error Change bfd_h_put_32
  1180. #endif
  1181.       if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer))
  1182.     return false;
  1183.  
  1184.       /* Handle long section names.  This code must handle section
  1185.      names just as they are handled in coff_write_object_contents.  */
  1186.       if (bfd_coff_long_section_names (abfd))
  1187.     {
  1188.       asection *o;
  1189.  
  1190.       for (o = abfd->sections; o != NULL; o = o->next)
  1191.         {
  1192.           size_t len;
  1193.  
  1194.           len = strlen (o->name);
  1195.           if (len > SCNNMLEN)
  1196.         {
  1197.           if (bfd_write (o->name, 1, len + 1, abfd) != len + 1)
  1198.             return false;
  1199.         }
  1200.         }
  1201.     }
  1202.  
  1203.       for (p = abfd->outsymbols, i = 0;
  1204.        i < limit;
  1205.        i++, p++)
  1206.     {
  1207.       asymbol *q = *p;
  1208.       size_t name_length = strlen (q->name);
  1209.       coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
  1210.       size_t maxlen;
  1211.  
  1212.       /* Figure out whether the symbol name should go in the string
  1213.          table.  Symbol names that are short enough are stored
  1214.          directly in the syment structure.  File names permit a
  1215.          different, longer, length in the syment structure.  On
  1216.          XCOFF, some symbol names are stored in the .debug section
  1217.          rather than in the string table.  */
  1218.  
  1219.       if (c_symbol == NULL
  1220.           || c_symbol->native == NULL)
  1221.         {
  1222.           /* This is not a COFF symbol, so it certainly is not a
  1223.              file name, nor does it go in the .debug section.  */
  1224.           maxlen = SYMNMLEN;
  1225.         }
  1226.       else if (bfd_coff_symname_in_debug (abfd,
  1227.                           &c_symbol->native->u.syment))
  1228.         {
  1229.           /* This symbol name is in the XCOFF .debug section.
  1230.              Don't write it into the string table.  */
  1231.           maxlen = name_length;
  1232.         }
  1233.       else if (c_symbol->native->u.syment.n_sclass == C_FILE
  1234.            && c_symbol->native->u.syment.n_numaux > 0)
  1235.         maxlen = FILNMLEN;
  1236.       else
  1237.         maxlen = SYMNMLEN;
  1238.  
  1239.       if (name_length > maxlen)
  1240.         {
  1241.           if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
  1242.           != name_length + 1)
  1243.         return false;
  1244.         }
  1245.     }
  1246.     }
  1247.   else
  1248.     {
  1249.       /* We would normally not write anything here, but we'll write
  1250.          out 4 so that any stupid coff reader which tries to read the
  1251.          string table even when there isn't one won't croak.  */
  1252.       unsigned int size = STRING_SIZE_SIZE;
  1253.       bfd_byte buffer[STRING_SIZE_SIZE];
  1254.  
  1255. #if STRING_SIZE_SIZE == 4
  1256.       bfd_h_put_32 (abfd, size, buffer);
  1257. #else
  1258.  #error Change bfd_h_put_32
  1259. #endif
  1260.       if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd)
  1261.       != STRING_SIZE_SIZE)
  1262.     return false;
  1263.     }
  1264.  
  1265.   /* Make sure the .debug section was created to be the correct size.
  1266.      We should create it ourselves on the fly, but we don't because
  1267.      BFD won't let us write to any section until we know how large all
  1268.      the sections are.  We could still do it by making another pass
  1269.      over the symbols.  FIXME.  */
  1270.   BFD_ASSERT (debug_string_size == 0
  1271.           || (debug_string_section != (asection *) NULL
  1272.           && (BFD_ALIGN (debug_string_size,
  1273.                  1 << debug_string_section->alignment_power)
  1274.               == bfd_section_size (abfd, debug_string_section))));
  1275.  
  1276.   return true;
  1277. }
  1278.  
  1279. boolean
  1280. coff_write_linenumbers (abfd)
  1281.      bfd *abfd;
  1282. {
  1283.   asection *s;
  1284.   bfd_size_type linesz;
  1285.   PTR buff;
  1286.  
  1287.   linesz = bfd_coff_linesz (abfd);
  1288.   buff = bfd_alloc (abfd, linesz);
  1289.   if (!buff)
  1290.     return false;
  1291.   for (s = abfd->sections; s != (asection *) NULL; s = s->next)
  1292.     {
  1293.       if (s->lineno_count)
  1294.     {
  1295.       asymbol **q = abfd->outsymbols;
  1296.       if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
  1297.         return false;
  1298.       /* Find all the linenumbers in this section */
  1299.       while (*q)
  1300.         {
  1301.           asymbol *p = *q;
  1302.           if (p->section->output_section == s)
  1303.         {
  1304.           alent *l =
  1305.           BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
  1306.                 (bfd_asymbol_bfd (p), p));
  1307.           if (l)
  1308.             {
  1309.               /* Found a linenumber entry, output */
  1310.               struct internal_lineno out;
  1311.               memset ((PTR) & out, 0, sizeof (out));
  1312.               out.l_lnno = 0;
  1313.               out.l_addr.l_symndx = l->u.offset;
  1314.               bfd_coff_swap_lineno_out (abfd, &out, buff);
  1315.               if (bfd_write (buff, 1, linesz, abfd) != linesz)
  1316.             return false;
  1317.               l++;
  1318.               while (l->line_number)
  1319.             {
  1320.               out.l_lnno = l->line_number;
  1321.               out.l_addr.l_symndx = l->u.offset;
  1322.               bfd_coff_swap_lineno_out (abfd, &out, buff);
  1323.               if (bfd_write (buff, 1, linesz, abfd) != linesz)
  1324.                 return false;
  1325.               l++;
  1326.             }
  1327.             }
  1328.         }
  1329.           q++;
  1330.         }
  1331.     }
  1332.     }
  1333.   bfd_release (abfd, buff);
  1334.   return true;
  1335. }
  1336.  
  1337. /*ARGSUSED */
  1338. alent *
  1339. coff_get_lineno (ignore_abfd, symbol)
  1340.      bfd *ignore_abfd;
  1341.      asymbol *symbol;
  1342. {
  1343.   return coffsymbol (symbol)->lineno;
  1344. }
  1345.  
  1346. #if 0
  1347.  
  1348. /* This is only called from coff_add_missing_symbols, which has been
  1349.    disabled.  */
  1350.  
  1351. asymbol *
  1352. coff_section_symbol (abfd, name)
  1353.      bfd *abfd;
  1354.      char *name;
  1355. {
  1356.   asection *sec = bfd_make_section_old_way (abfd, name);
  1357.   asymbol *sym;
  1358.   combined_entry_type *csym;
  1359.  
  1360.   sym = sec->symbol;
  1361.   csym = coff_symbol_from (abfd, sym)->native;
  1362.   /* Make sure back-end COFF stuff is there.  */
  1363.   if (csym == 0)
  1364.     {
  1365.       struct foo
  1366.     {
  1367.       coff_symbol_type sym;
  1368.       /* @@FIXME This shouldn't use a fixed size!!  */
  1369.       combined_entry_type e[10];
  1370.     };
  1371.       struct foo *f;
  1372.       f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f));
  1373.       if (!f)
  1374.     {
  1375.       bfd_set_error (bfd_error_no_error);
  1376.       return NULL;
  1377.     }
  1378.       memset ((char *) f, 0, sizeof (*f));
  1379.       coff_symbol_from (abfd, sym)->native = csym = f->e;
  1380.     }
  1381.   csym[0].u.syment.n_sclass = C_STAT;
  1382.   csym[0].u.syment.n_numaux = 1;
  1383. /*  SF_SET_STATICS (sym);       @@ ??? */
  1384.   csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
  1385.   csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
  1386.   csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
  1387.  
  1388.   if (sec->output_section == NULL)
  1389.     {
  1390.       sec->output_section = sec;
  1391.       sec->output_offset = 0;
  1392.     }
  1393.  
  1394.   return sym;
  1395. }
  1396.  
  1397. #endif /* 0 */
  1398.  
  1399. /* This function transforms the offsets into the symbol table into
  1400.    pointers to syments.  */
  1401.  
  1402. static void
  1403. coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
  1404.      bfd *abfd;
  1405.      combined_entry_type *table_base;
  1406.      combined_entry_type *symbol;
  1407.      unsigned int indaux;
  1408.      combined_entry_type *auxent;
  1409. {
  1410.   int type = symbol->u.syment.n_type;
  1411.   int class = symbol->u.syment.n_sclass;
  1412.  
  1413.   if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
  1414.     {
  1415.       if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
  1416.       (abfd, table_base, symbol, indaux, auxent))
  1417.     return;
  1418.     }
  1419.  
  1420.   /* Don't bother if this is a file or a section */
  1421.   if (class == C_STAT && type == T_NULL)
  1422.     return;
  1423.   if (class == C_FILE)
  1424.     return;
  1425.  
  1426.   /* Otherwise patch up */
  1427. #define N_TMASK coff_data (abfd)->local_n_tmask
  1428. #define N_BTSHFT coff_data (abfd)->local_n_btshft
  1429.   if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK || class == C_FCN)
  1430.       && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
  1431.     {
  1432.       auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
  1433.     table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
  1434.       auxent->fix_end = 1;
  1435.     }
  1436.   /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
  1437.      generate one, so we must be careful to ignore it.  */
  1438.   if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
  1439.     {
  1440.       auxent->u.auxent.x_sym.x_tagndx.p =
  1441.     table_base + auxent->u.auxent.x_sym.x_tagndx.l;
  1442.       auxent->fix_tag = 1;
  1443.     }
  1444. }
  1445.  
  1446. /* Allocate space for the ".debug" section, and read it.
  1447.    We did not read the debug section until now, because
  1448.    we didn't want to go to the trouble until someone needed it. */
  1449.  
  1450. static char *
  1451. build_debug_section (abfd)
  1452.      bfd *abfd;
  1453. {
  1454.   char *debug_section;
  1455.   long position;
  1456.  
  1457.   asection *sect = bfd_get_section_by_name (abfd, ".debug");
  1458.  
  1459.   if (!sect)
  1460.     {
  1461.       bfd_set_error (bfd_error_no_debug_section);
  1462.       return NULL;
  1463.     }
  1464.  
  1465.   debug_section = (PTR) bfd_alloc (abfd,
  1466.                    bfd_get_section_size_before_reloc (sect));
  1467.   if (debug_section == NULL)
  1468.     return NULL;
  1469.  
  1470.   /* Seek to the beginning of the `.debug' section and read it. 
  1471.      Save the current position first; it is needed by our caller.
  1472.      Then read debug section and reset the file pointer.  */
  1473.  
  1474.   position = bfd_tell (abfd);
  1475.   if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
  1476.       || (bfd_read (debug_section,
  1477.             bfd_get_section_size_before_reloc (sect), 1, abfd)
  1478.       != bfd_get_section_size_before_reloc (sect))
  1479.       || bfd_seek (abfd, position, SEEK_SET) != 0)
  1480.     return NULL;
  1481.   return debug_section;
  1482. }
  1483.  
  1484.  
  1485. /* Return a pointer to a malloc'd copy of 'name'.  'name' may not be
  1486.    \0-terminated, but will not exceed 'maxlen' characters.  The copy *will*
  1487.    be \0-terminated.  */
  1488. static char *
  1489. copy_name (abfd, name, maxlen)
  1490.      bfd *abfd;
  1491.      char *name;
  1492.      int maxlen;
  1493. {
  1494.   int len;
  1495.   char *newname;
  1496.  
  1497.   for (len = 0; len < maxlen; ++len)
  1498.     {
  1499.       if (name[len] == '\0')
  1500.     {
  1501.       break;
  1502.     }
  1503.     }
  1504.  
  1505.   if ((newname = (PTR) bfd_alloc (abfd, len + 1)) == NULL)
  1506.     return (NULL);
  1507.   strncpy (newname, name, len);
  1508.   newname[len] = '\0';
  1509.   return newname;
  1510. }
  1511.  
  1512. /* Read in the external symbols.  */
  1513.  
  1514. boolean
  1515. _bfd_coff_get_external_symbols (abfd)
  1516.      bfd *abfd;
  1517. {
  1518.   bfd_size_type symesz;
  1519.   size_t size;
  1520.   PTR syms;
  1521.  
  1522.   if (obj_coff_external_syms (abfd) != NULL)
  1523.     return true;
  1524.  
  1525.   symesz = bfd_coff_symesz (abfd);
  1526.  
  1527.   size = obj_raw_syment_count (abfd) * symesz;
  1528.  
  1529.   syms = (PTR) bfd_malloc (size);
  1530.   if (syms == NULL && size != 0)
  1531.     return false;
  1532.  
  1533.   if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
  1534.       || bfd_read (syms, size, 1, abfd) != size)
  1535.     {
  1536.       if (syms != NULL)
  1537.     free (syms);
  1538.       return false;
  1539.     }
  1540.  
  1541.   obj_coff_external_syms (abfd) = syms;
  1542.  
  1543.   return true;
  1544. }
  1545.  
  1546. /* Read in the external strings.  The strings are not loaded until
  1547.    they are needed.  This is because we have no simple way of
  1548.    detecting a missing string table in an archive.  */
  1549.  
  1550. const char *
  1551. _bfd_coff_read_string_table (abfd)
  1552.      bfd *abfd;
  1553. {
  1554.   char extstrsize[STRING_SIZE_SIZE];
  1555.   size_t strsize;
  1556.   char *strings;
  1557.  
  1558.   if (obj_coff_strings (abfd) != NULL)
  1559.     return obj_coff_strings (abfd);
  1560.  
  1561.   if (obj_sym_filepos (abfd) == 0)
  1562.     {
  1563.       bfd_set_error (bfd_error_no_symbols);
  1564.       return NULL;
  1565.     }
  1566.  
  1567.   if (bfd_seek (abfd,
  1568.         (obj_sym_filepos (abfd)
  1569.          + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
  1570.         SEEK_SET) != 0)
  1571.     return NULL;
  1572.     
  1573.   if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
  1574.     {
  1575.       if (bfd_get_error () != bfd_error_file_truncated)
  1576.     return NULL;
  1577.  
  1578.       /* There is no string table.  */
  1579.       strsize = STRING_SIZE_SIZE;
  1580.     }
  1581.   else
  1582.     {
  1583. #if STRING_SIZE_SIZE == 4
  1584.       strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize);
  1585. #else
  1586.  #error Change bfd_h_get_32
  1587. #endif
  1588.     }
  1589.  
  1590.   if (strsize < STRING_SIZE_SIZE)
  1591.     {
  1592.       (*_bfd_error_handler)
  1593.     ("%s: bad string table size %lu", bfd_get_filename (abfd),
  1594.      (unsigned long) strsize);
  1595.       bfd_set_error (bfd_error_bad_value);
  1596.       return NULL;
  1597.     }
  1598.  
  1599.   strings = (char *) bfd_malloc (strsize);
  1600.   if (strings == NULL)
  1601.     return NULL;
  1602.  
  1603.   if (bfd_read (strings + STRING_SIZE_SIZE,
  1604.         strsize - STRING_SIZE_SIZE, 1, abfd)
  1605.       != strsize - STRING_SIZE_SIZE)
  1606.     {
  1607.       free (strings);
  1608.       return NULL;
  1609.     }
  1610.  
  1611.   obj_coff_strings (abfd) = strings;
  1612.  
  1613.   return strings;
  1614. }
  1615.  
  1616. /* Free up the external symbols and strings read from a COFF file.  */
  1617.  
  1618. boolean
  1619. _bfd_coff_free_symbols (abfd)
  1620.      bfd *abfd;
  1621. {
  1622.   if (obj_coff_external_syms (abfd) != NULL
  1623.       && ! obj_coff_keep_syms (abfd))
  1624.     {
  1625.       free (obj_coff_external_syms (abfd));
  1626.       obj_coff_external_syms (abfd) = NULL;
  1627.     }
  1628.   if (obj_coff_strings (abfd) != NULL
  1629.       && ! obj_coff_keep_strings (abfd))
  1630.     {
  1631.       free (obj_coff_strings (abfd));
  1632.       obj_coff_strings (abfd) = NULL;
  1633.     }
  1634.   return true;
  1635. }
  1636.  
  1637. /* Read a symbol table into freshly bfd_allocated memory, swap it, and
  1638.    knit the symbol names into a normalized form.  By normalized here I
  1639.    mean that all symbols have an n_offset pointer that points to a null-
  1640.    terminated string.  */
  1641.  
  1642. combined_entry_type *
  1643. coff_get_normalized_symtab (abfd)
  1644.      bfd *abfd;
  1645. {
  1646.   combined_entry_type *internal;
  1647.   combined_entry_type *internal_ptr;
  1648.   combined_entry_type *symbol_ptr;
  1649.   combined_entry_type *internal_end;
  1650.   bfd_size_type symesz;
  1651.   char *raw_src;
  1652.   char *raw_end;
  1653.   const char *string_table = NULL;
  1654.   char *debug_section = NULL;
  1655.   unsigned long size;
  1656.  
  1657.   if (obj_raw_syments (abfd) != NULL)
  1658.     return obj_raw_syments (abfd);
  1659.  
  1660.   size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
  1661.   internal = (combined_entry_type *) bfd_zalloc (abfd, size);
  1662.   if (internal == NULL && size != 0)
  1663.     return NULL;
  1664.   internal_end = internal + obj_raw_syment_count (abfd);
  1665.  
  1666.   if (! _bfd_coff_get_external_symbols (abfd))
  1667.     return NULL;
  1668.  
  1669.   raw_src = (char *) obj_coff_external_syms (abfd);
  1670.  
  1671.   /* mark the end of the symbols */
  1672.   symesz = bfd_coff_symesz (abfd);
  1673.   raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
  1674.  
  1675.   /* FIXME SOMEDAY.  A string table size of zero is very weird, but
  1676.      probably possible.  If one shows up, it will probably kill us.  */
  1677.  
  1678.   /* Swap all the raw entries */
  1679.   for (internal_ptr = internal;
  1680.        raw_src < raw_end;
  1681.        raw_src += symesz, internal_ptr++)
  1682.     {
  1683.  
  1684.       unsigned int i;
  1685.       bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
  1686.                 (PTR) & internal_ptr->u.syment);
  1687.       symbol_ptr = internal_ptr;
  1688.  
  1689.       for (i = 0;
  1690.        i < symbol_ptr->u.syment.n_numaux;
  1691.        i++)
  1692.     {
  1693.       internal_ptr++;
  1694.       raw_src += symesz;
  1695.       bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
  1696.                 symbol_ptr->u.syment.n_type,
  1697.                 symbol_ptr->u.syment.n_sclass,
  1698.                 i, symbol_ptr->u.syment.n_numaux,
  1699.                 &(internal_ptr->u.auxent));
  1700.       coff_pointerize_aux (abfd, internal, symbol_ptr, i,
  1701.                    internal_ptr);
  1702.     }
  1703.     }
  1704.  
  1705.   /* Free the raw symbols, but not the strings (if we have them).  */
  1706.   obj_coff_keep_strings (abfd) = true;
  1707.   if (! _bfd_coff_free_symbols (abfd))
  1708.     return NULL;
  1709.  
  1710.   for (internal_ptr = internal; internal_ptr < internal_end;
  1711.        internal_ptr++)
  1712.     {
  1713.       if (internal_ptr->u.syment.n_sclass == C_FILE
  1714.       && internal_ptr->u.syment.n_numaux > 0)
  1715.     {
  1716.       /* make a file symbol point to the name in the auxent, since
  1717.          the text ".file" is redundant */
  1718.       if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
  1719.         {
  1720.           /* the filename is a long one, point into the string table */
  1721.           if (string_table == NULL)
  1722.         {
  1723.           string_table = _bfd_coff_read_string_table (abfd);
  1724.           if (string_table == NULL)
  1725.             return NULL;
  1726.         }
  1727.  
  1728.           internal_ptr->u.syment._n._n_n._n_offset =
  1729.         ((long)
  1730.          (string_table
  1731.           + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
  1732.         }
  1733.       else
  1734.         {
  1735.           /* ordinary short filename, put into memory anyway */
  1736.           internal_ptr->u.syment._n._n_n._n_offset = (long)
  1737.         copy_name (abfd, (internal_ptr + 1)->u.auxent.x_file.x_fname,
  1738.                FILNMLEN);
  1739.         }
  1740.     }
  1741.       else
  1742.     {
  1743.       if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
  1744.         {
  1745.           /* This is a "short" name.  Make it long.  */
  1746.           unsigned long i = 0;
  1747.           char *newstring = NULL;
  1748.  
  1749.           /* find the length of this string without walking into memory
  1750.              that isn't ours.  */
  1751.           for (i = 0; i < 8; ++i)
  1752.         {
  1753.           if (internal_ptr->u.syment._n._n_name[i] == '\0')
  1754.             {
  1755.               break;
  1756.             }        /* if end of string */
  1757.         }        /* possible lengths of this string. */
  1758.  
  1759.           if ((newstring = (PTR) bfd_alloc (abfd, ++i)) == NULL)
  1760.         return (NULL);
  1761.           memset (newstring, 0, i);
  1762.           strncpy (newstring, internal_ptr->u.syment._n._n_name, i - 1);
  1763.           internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
  1764.           internal_ptr->u.syment._n._n_n._n_zeroes = 0;
  1765.         }
  1766.       else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
  1767.         internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
  1768.       else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
  1769.         {
  1770.           /* Long name already.  Point symbol at the string in the
  1771.                  table.  */
  1772.           if (string_table == NULL)
  1773.         {
  1774.           string_table = _bfd_coff_read_string_table (abfd);
  1775.           if (string_table == NULL)
  1776.             return NULL;
  1777.         }
  1778.           internal_ptr->u.syment._n._n_n._n_offset =
  1779.         ((long int)
  1780.          (string_table
  1781.           + internal_ptr->u.syment._n._n_n._n_offset));
  1782.         }
  1783.       else
  1784.         {
  1785.           /* Long name in debug section.  Very similar.  */
  1786.           if (debug_section == NULL)
  1787.         debug_section = build_debug_section (abfd);
  1788.           internal_ptr->u.syment._n._n_n._n_offset = (long int)
  1789.         (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
  1790.         }
  1791.     }
  1792.       internal_ptr += internal_ptr->u.syment.n_numaux;
  1793.     }
  1794.  
  1795.   obj_raw_syments (abfd) = internal;
  1796.   BFD_ASSERT (obj_raw_syment_count (abfd)
  1797.           == (unsigned int) (internal_ptr - internal));
  1798.  
  1799.   return (internal);
  1800. }                /* coff_get_normalized_symtab() */
  1801.  
  1802. long
  1803. coff_get_reloc_upper_bound (abfd, asect)
  1804.      bfd *abfd;
  1805.      sec_ptr asect;
  1806. {
  1807.   if (bfd_get_format (abfd) != bfd_object)
  1808.     {
  1809.       bfd_set_error (bfd_error_invalid_operation);
  1810.       return -1;
  1811.     }
  1812.   return (asect->reloc_count + 1) * sizeof (arelent *);
  1813. }
  1814.  
  1815. asymbol *
  1816. coff_make_empty_symbol (abfd)
  1817.      bfd *abfd;
  1818. {
  1819.   coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
  1820.   if (new == NULL)
  1821.     return (NULL);
  1822.   memset (new, 0, sizeof *new);
  1823.   new->symbol.section = 0;
  1824.   new->native = 0;
  1825.   new->lineno = (alent *) NULL;
  1826.   new->done_lineno = false;
  1827.   new->symbol.the_bfd = abfd;
  1828.   return &new->symbol;
  1829. }
  1830.  
  1831. /* Make a debugging symbol.  */
  1832.  
  1833. asymbol *
  1834. coff_bfd_make_debug_symbol (abfd, ptr, sz)
  1835.      bfd *abfd;
  1836.      PTR ptr;
  1837.      unsigned long sz;
  1838. {
  1839.   coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
  1840.   if (new == NULL)
  1841.     return (NULL);
  1842.   /* @@ The 10 is a guess at a plausible maximum number of aux entries
  1843.      (but shouldn't be a constant).  */
  1844.   new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
  1845.   if (!new->native)
  1846.     return (NULL);
  1847.   new->symbol.section = bfd_abs_section_ptr;
  1848.   new->symbol.flags = BSF_DEBUGGING;
  1849.   new->lineno = (alent *) NULL;
  1850.   new->done_lineno = false;
  1851.   new->symbol.the_bfd = abfd;
  1852.   return &new->symbol;
  1853. }
  1854.  
  1855. /*ARGSUSED */
  1856. void
  1857. coff_get_symbol_info (abfd, symbol, ret)
  1858.      bfd *abfd;
  1859.      asymbol *symbol;
  1860.      symbol_info *ret;
  1861. {
  1862.   bfd_symbol_info (symbol, ret);
  1863.   if (coffsymbol (symbol)->native != NULL
  1864.       && coffsymbol (symbol)->native->fix_value)
  1865.     {
  1866.       combined_entry_type *psym;
  1867.  
  1868.       psym = ((combined_entry_type *)
  1869.           coffsymbol (symbol)->native->u.syment.n_value);
  1870.       ret->value = (bfd_vma) (psym - obj_raw_syments (abfd));
  1871.     }
  1872. }
  1873.  
  1874. /* Return the COFF syment for a symbol.  */
  1875.  
  1876. boolean
  1877. bfd_coff_get_syment (abfd, symbol, psyment)
  1878.      bfd *abfd;
  1879.      asymbol *symbol;
  1880.      struct internal_syment *psyment;
  1881. {
  1882.   coff_symbol_type *csym;
  1883.  
  1884.   csym = coff_symbol_from (abfd, symbol);
  1885.   if (csym == NULL || csym->native == NULL)
  1886.     {
  1887.       bfd_set_error (bfd_error_invalid_operation);
  1888.       return false;
  1889.     }
  1890.  
  1891.   *psyment = csym->native->u.syment;
  1892.  
  1893.   if (csym->native->fix_value)
  1894.     psyment->n_value = ((combined_entry_type *) psyment->n_value
  1895.             - obj_raw_syments (abfd));
  1896.  
  1897.   /* FIXME: We should handle fix_line here.  */
  1898.  
  1899.   return true;
  1900. }
  1901.  
  1902. /* Return the COFF auxent for a symbol.  */
  1903.  
  1904. boolean
  1905. bfd_coff_get_auxent (abfd, symbol, indx, pauxent)
  1906.      bfd *abfd;
  1907.      asymbol *symbol;
  1908.      int indx;
  1909.      union internal_auxent *pauxent;
  1910. {
  1911.   coff_symbol_type *csym;
  1912.   combined_entry_type *ent;
  1913.  
  1914.   csym = coff_symbol_from (abfd, symbol);
  1915.  
  1916.   if (csym == NULL
  1917.       || csym->native == NULL
  1918.       || indx >= csym->native->u.syment.n_numaux)
  1919.     {
  1920.       bfd_set_error (bfd_error_invalid_operation);
  1921.       return false;
  1922.     }
  1923.  
  1924.   ent = csym->native + indx + 1;
  1925.  
  1926.   *pauxent = ent->u.auxent;
  1927.  
  1928.   if (ent->fix_tag)
  1929.     pauxent->x_sym.x_tagndx.l =
  1930.       ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
  1931.        - obj_raw_syments (abfd));
  1932.  
  1933.   if (ent->fix_end)
  1934.     pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
  1935.       ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
  1936.        - obj_raw_syments (abfd));
  1937.  
  1938.   if (ent->fix_scnlen)
  1939.     pauxent->x_csect.x_scnlen.l =
  1940.       ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
  1941.        - obj_raw_syments (abfd));
  1942.  
  1943.   return true;
  1944. }
  1945.  
  1946. /* Print out information about COFF symbol.  */
  1947.  
  1948. void
  1949. coff_print_symbol (abfd, filep, symbol, how)
  1950.      bfd *abfd;
  1951.      PTR filep;
  1952.      asymbol *symbol;
  1953.      bfd_print_symbol_type how;
  1954. {
  1955.   FILE *file = (FILE *) filep;
  1956.  
  1957.   switch (how)
  1958.     {
  1959.     case bfd_print_symbol_name:
  1960.       fprintf (file, "%s", symbol->name);
  1961.       break;
  1962.  
  1963.     case bfd_print_symbol_more:
  1964.       fprintf (file, "coff %s %s",
  1965.            coffsymbol (symbol)->native ? "n" : "g",
  1966.            coffsymbol (symbol)->lineno ? "l" : " ");
  1967.       break;
  1968.  
  1969.     case bfd_print_symbol_all:
  1970.       if (coffsymbol (symbol)->native)
  1971.     {
  1972.       unsigned long val;
  1973.       unsigned int aux;
  1974.       combined_entry_type *combined = coffsymbol (symbol)->native;
  1975.       combined_entry_type *root = obj_raw_syments (abfd);
  1976.       struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
  1977.  
  1978.       fprintf (file, "[%3ld]", (long) (combined - root));
  1979.  
  1980.       if (! combined->fix_value)
  1981.         val = (unsigned long) combined->u.syment.n_value;
  1982.       else
  1983.         val = ((unsigned long)
  1984.            ((combined_entry_type *) combined->u.syment.n_value
  1985.             - root));
  1986.  
  1987.       fprintf (file,
  1988.            "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
  1989.            combined->u.syment.n_scnum,
  1990.            combined->u.syment.n_flags,
  1991.            combined->u.syment.n_type,
  1992.            combined->u.syment.n_sclass,
  1993.            combined->u.syment.n_numaux,
  1994.            val,
  1995.            symbol->name);
  1996.  
  1997.       for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
  1998.         {
  1999.           combined_entry_type *auxp = combined + aux + 1;
  2000.           long tagndx;
  2001.  
  2002.           if (auxp->fix_tag)
  2003.         tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
  2004.           else
  2005.         tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
  2006.  
  2007.           fprintf (file, "\n");
  2008.  
  2009.           if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
  2010.         continue;
  2011.  
  2012.           switch (combined->u.syment.n_sclass)
  2013.         {
  2014.         case C_FILE:
  2015.           fprintf (file, "File ");
  2016.           break;
  2017.  
  2018.         case C_STAT:
  2019.           if (combined->u.syment.n_type == T_NULL)
  2020.             /* probably a section symbol? */
  2021.             {
  2022.               fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
  2023.                    (long) auxp->u.auxent.x_scn.x_scnlen,
  2024.                    auxp->u.auxent.x_scn.x_nreloc,
  2025.                    auxp->u.auxent.x_scn.x_nlinno);
  2026.               if (auxp->u.auxent.x_scn.x_checksum != 0
  2027.               || auxp->u.auxent.x_scn.x_associated != 0
  2028.               || auxp->u.auxent.x_scn.x_comdat != 0)
  2029.             fprintf (file, " checksum 0x%lx assoc %d comdat %d",
  2030.                  auxp->u.auxent.x_scn.x_checksum,
  2031.                  auxp->u.auxent.x_scn.x_associated,
  2032.                  auxp->u.auxent.x_scn.x_comdat);
  2033.               break;
  2034.             }
  2035.           /* else fall through */
  2036.  
  2037.         default:
  2038.           fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
  2039.                auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
  2040.                auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
  2041.                tagndx);
  2042.           if (auxp->fix_end)
  2043.             fprintf (file, " endndx %ld",
  2044.                  ((long)
  2045.                   (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
  2046.                    - root)));
  2047.           break;
  2048.         }
  2049.         }
  2050.  
  2051.       if (l)
  2052.         {
  2053.           fprintf (file, "\n%s :", l->u.sym->name);
  2054.           l++;
  2055.           while (l->line_number)
  2056.         {
  2057.           fprintf (file, "\n%4d : 0x%lx",
  2058.                l->line_number,
  2059.                ((unsigned long)
  2060.                 (l->u.offset + symbol->section->vma)));
  2061.           l++;
  2062.         }
  2063.         }
  2064.     }
  2065.       else
  2066.     {
  2067.       bfd_print_symbol_vandf ((PTR) file, symbol);
  2068.       fprintf (file, " %-5s %s %s %s",
  2069.            symbol->section->name,
  2070.            coffsymbol (symbol)->native ? "n" : "g",
  2071.            coffsymbol (symbol)->lineno ? "l" : " ",
  2072.            symbol->name);
  2073.     }
  2074.     }
  2075. }
  2076.  
  2077. /* Provided a BFD, a section and an offset into the section, calculate
  2078.    and return the name of the source file and the line nearest to the
  2079.    wanted location.  */
  2080.  
  2081. /*ARGSUSED*/
  2082. boolean
  2083. coff_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
  2084.             functionname_ptr, line_ptr)
  2085.      bfd *abfd;
  2086.      asection *section;
  2087.      asymbol **symbols;
  2088.      bfd_vma offset;
  2089.      CONST char **filename_ptr;
  2090.      CONST char **functionname_ptr;
  2091.      unsigned int *line_ptr;
  2092. {
  2093.   boolean found;
  2094.   unsigned int i;
  2095.   unsigned int line_base;
  2096.   coff_data_type *cof = coff_data (abfd);
  2097.   /* Run through the raw syments if available */
  2098.   combined_entry_type *p;
  2099.   combined_entry_type *pend;
  2100.   alent *l;
  2101.   struct coff_section_tdata *sec_data;
  2102.  
  2103.   /* Before looking through the symbol table, try to use a .stab
  2104.      section to find the information.  */
  2105.   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
  2106.                          &found, filename_ptr,
  2107.                          functionname_ptr, line_ptr,
  2108.                          &coff_data (abfd)->line_info))
  2109.     return false;
  2110.   if (found)
  2111.     return true;
  2112.  
  2113.   *filename_ptr = 0;
  2114.   *functionname_ptr = 0;
  2115.   *line_ptr = 0;
  2116.  
  2117.   /* Don't try and find line numbers in a non coff file */
  2118.   if (abfd->xvec->flavour != bfd_target_coff_flavour)
  2119.     return false;
  2120.  
  2121.   if (cof == NULL)
  2122.     return false;
  2123.  
  2124.   /* Find the first C_FILE symbol.  */
  2125.   p = cof->raw_syments;
  2126.   pend = p + cof->raw_syment_count;
  2127.   while (p < pend)
  2128.     {
  2129.       if (p->u.syment.n_sclass == C_FILE)
  2130.     break;
  2131.       p += 1 + p->u.syment.n_numaux;
  2132.     }
  2133.  
  2134.   if (p < pend)
  2135.     {
  2136.       bfd_vma maxdiff;
  2137.  
  2138.       /* Look through the C_FILE symbols to find the best one.  */
  2139.       *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
  2140.       maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
  2141.       while (1)
  2142.     {
  2143.       combined_entry_type *p2;
  2144.  
  2145.       for (p2 = p + 1 + p->u.syment.n_numaux;
  2146.            p2 < pend;
  2147.            p2 += 1 + p2->u.syment.n_numaux)
  2148.         {
  2149.           if (p2->u.syment.n_scnum > 0
  2150.           && (section
  2151.               == coff_section_from_bfd_index (abfd,
  2152.                               p2->u.syment.n_scnum)))
  2153.         break;
  2154.           if (p2->u.syment.n_sclass == C_FILE)
  2155.         {
  2156.           p2 = pend;
  2157.           break;
  2158.         }
  2159.         }
  2160.  
  2161.       if (p2 < pend
  2162.           && offset >= (bfd_vma) p2->u.syment.n_value
  2163.           && offset - (bfd_vma) p2->u.syment.n_value < maxdiff)
  2164.         {
  2165.           *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
  2166.           maxdiff = offset - p2->u.syment.n_value;
  2167.         }
  2168.  
  2169.       /* Avoid endless loops on erroneous files by ensuring that
  2170.          we always move forward in the file.  */
  2171.       if (p - cof->raw_syments >= p->u.syment.n_value)
  2172.         break;
  2173.  
  2174.       p = cof->raw_syments + p->u.syment.n_value;
  2175.       if (p > pend || p->u.syment.n_sclass != C_FILE)
  2176.         break;
  2177.     }
  2178.     }
  2179.  
  2180.   /* Now wander though the raw linenumbers of the section */
  2181.   /* If we have been called on this section before, and the offset we
  2182.      want is further down then we can prime the lookup loop.  */
  2183.   sec_data = coff_section_data (abfd, section);
  2184.   if (sec_data != NULL
  2185.       && sec_data->i > 0
  2186.       && offset >= sec_data->offset)
  2187.     {
  2188.       i = sec_data->i;
  2189.       *functionname_ptr = sec_data->function;
  2190.       line_base = sec_data->line_base;
  2191.     }
  2192.   else
  2193.     {
  2194.       i = 0;
  2195.       line_base = 0;
  2196.     }
  2197.  
  2198.   if (section->lineno != NULL)
  2199.     {
  2200.       l = §ion->lineno[i];
  2201.  
  2202.       for (; i < section->lineno_count; i++)
  2203.     {
  2204.       if (l->line_number == 0)
  2205.         {
  2206.           /* Get the symbol this line number points at */
  2207.           coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
  2208.           if (coff->symbol.value > offset)
  2209.         break;
  2210.           *functionname_ptr = coff->symbol.name;
  2211.           if (coff->native)
  2212.         {
  2213.           combined_entry_type *s = coff->native;
  2214.           s = s + 1 + s->u.syment.n_numaux;
  2215.  
  2216.           /* In XCOFF a debugging symbol can follow the
  2217.              function symbol.  */
  2218.           if (s->u.syment.n_scnum == N_DEBUG)
  2219.             s = s + 1 + s->u.syment.n_numaux;
  2220.  
  2221.           /* S should now point to the .bf of the function.  */
  2222.           if (s->u.syment.n_numaux)
  2223.             {
  2224.               /* The linenumber is stored in the auxent.  */
  2225.               union internal_auxent *a = &((s + 1)->u.auxent);
  2226.               line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
  2227.               *line_ptr = line_base;
  2228.             }
  2229.         }
  2230.         }
  2231.       else
  2232.         {
  2233.           if (l->u.offset + bfd_get_section_vma (abfd, section) > offset)
  2234.         break;
  2235.           *line_ptr = l->line_number + line_base - 1;
  2236.         }
  2237.       l++;
  2238.     }
  2239.     }
  2240.  
  2241.   /* Cache the results for the next call.  */
  2242.   if (sec_data == NULL && section->owner == abfd)
  2243.     {
  2244.       section->used_by_bfd =
  2245.     ((PTR) bfd_zalloc (abfd,
  2246.                sizeof (struct coff_section_tdata)));
  2247.       sec_data = (struct coff_section_tdata *) section->used_by_bfd;
  2248.     }
  2249.   if (sec_data != NULL)
  2250.     {
  2251.       sec_data->offset = offset;
  2252.       sec_data->i = i;
  2253.       sec_data->function = *functionname_ptr;
  2254.       sec_data->line_base = line_base;
  2255.     }
  2256.  
  2257.   return true;
  2258. }
  2259.  
  2260. int
  2261. coff_sizeof_headers (abfd, reloc)
  2262.      bfd *abfd;
  2263.      boolean reloc;
  2264. {
  2265.   size_t size;
  2266.  
  2267.   if (reloc == false)
  2268.     {
  2269.       size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
  2270.     }
  2271.   else
  2272.     {
  2273.       size = bfd_coff_filhsz (abfd);
  2274.     }
  2275.  
  2276.   size += abfd->section_count * bfd_coff_scnhsz (abfd);
  2277.   return size;
  2278. }
  2279.